#include <stdio.h>

int count_num(int n)
{
    int count = 0;

    while (n) {
        if (n%10 == 1)
            count ++;
        n = n/10;
    }

    return count;
}

int count_1(int n)
{
    int i, count=0;

    for (i = 1; i <= n; i++) {
        count += count_num(i); 
    }

    return count;
}

int count_2(int n)
{
    int m = n, count = 0, a = 10, b = 1, t;
    while (m) {
        t = (n%a-b+1)>b?b:(n%a-b+1);
        count += (n/a)*b + (t>0?t:0); 
        m = m/10;
        a = a*10;
        b = b*10;
    }
    
    return count;
}

int main(int argc, char *argv[])
{
    int n = 13;
    printf("num %d count %d\n", n, count_1(n));
    printf("num %d count %d\n", n, count_2(n));

    return 0;
}
